home *** CD-ROM | disk | FTP | other *** search
- Path: user2.mnsinc.com!huang
- From: huang@mnsinc.com (Szu-Wen Huang)
- Newsgroups: comp.lang.c
- Subject: Re: [Q] unpack 10 bit data
- Date: 18 Apr 1996 17:34:46 GMT
- Organization: Monumental Network Systems
- Message-ID: <4l5ufn$79p@news1.mnsinc.com>
- References: <Pine.SGI.3.91.960417231151.16763A-100000@anchor.gsfc.nasa.gov> <829831450snz@genesis.demon.co.uk>
- NNTP-Posting-Host: user.mnsinc.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Lawrence Kirby (fred@genesis.demon.co.uk) wrote:
- : In article <Pine.SGI.3.91.960417231151.16763A-100000@anchor.gsfc.nasa.gov>
- : jyli@anchor.gsfc.nasa.gov "Jason Y. Li" writes:
-
- : >I am looking for an efficient and elegant way of unpacking 10 bit data.
- : >In many scientific applications, raw data are packed into 10 bit data
- : >stream. For example, an input buffer of 40 bits (5 characters) contains
- : >four 10-bit integers.
- : > I know one way of unpacking the data which involves
- : >bit masking and then shifting left-and-right. It looks pretty messy to me. I
- : >am sure smart folks out there have better ways or ideas. I'd appreciate
- : >if you would let me know.
-
- : The solution will most likely involve shifting and masking. However there
- : may perhaps be a cleaner solution than the one you saw.
-
- This is strictly for the purpose of giving you an alternative solution,
- and may or may not be better than the mask/shift approach. Theoretically,
- if you had enough memory, you can build a huge look-up table indexed by
- the 40-bit key. Each entry in the table will the contain the ordered
- quadruples of 10-bit integers. I doubt even NASA would devote this kind
- of memory to solve this triviality :)
-
- Therefore, we compromise. You can take the first 2 bytes and use it to
- index table 1, which contains the first 10-bit integer. Then, take the
- second and the third bytes to index table 2, another table that gives
- you your second 10-bit integer. Then bytes 3 and 4, then 4 and 5. You
- will need 4 tables of 64K elements each, 10 bits per element (make it
- 16 to be easy). That takes about 512K of memory, and gives you the
- performance of requiring just one memory read per 10-bit integer.
- Comparing that with essentially 2 masks, 2 shifts, and an OR, this
- memory hog just might be useful - that is, if you really need it ;).
-